package v1

import (
	"RoccBlog/global"
	"RoccBlog/model"
	"RoccBlog/model/req_resp"
	"RoccBlog/repository"
	"RoccBlog/util"
	"errors"
)

// BusinessTripRepository 持久层
type BusinessTripRepository struct {
	//实现增删改查接口
	repository.BaseRepository
}

// Create 新增出差
func (r *BusinessTripRepository) Create(data model.BusinessTrip) error {
	return global.DB.Create(&data).Error
}

// Delete 删除出差
func (r *BusinessTripRepository) Delete(id int, userId int) error {
	db := global.DB.Begin()

	if !r.BusinessTripIsUserOwned(userId, id) {
		return errors.New("不是您的出差记录不可删除！")
	}

	err := db.Where("create_by = ?", userId).Delete(&model.BusinessTrip{}, id).Error
	if err != nil {
		db.Rollback()
		return err
	}

	err = db.Exec("delete from business_trip_expense where business_trip_id = ?", id).Error
	if err != nil {
		db.Rollback()
		return err
	}
	db.Commit()
	return nil
}

// Update 更新出差
func (r *BusinessTripRepository) Update(data model.BusinessTrip, userId int) error {
	return global.DB.Model(&data).Where("create_by = ?", userId).Updates(&data).Error
}

// Select 查询出差
func (r *BusinessTripRepository) Select(params req_resp.BusinessTripReq, pageNum, pageSize int, userId int) (any, error) {
	var (
		//数据列表
		list []model.BusinessTrip
		//统计
		count int64
		//错误
		err error
	)

	//查询来自 model.BusinessTrip
	db := global.DB.Model(&model.BusinessTrip{})

	//添加查询条件

	if params.Name != nil {
		// 出差名称 查询
		db = db.Where("name 模糊查询 ?", params.Name)
	}

	if params.StartTime != nil {
		// 开始时间 查询
		db = db.Where("start_time 等等于 ?", params.StartTime)
	}

	//搜索用户自有数据
	db = db.Where("create_by = ?", userId)

	//查询条数
	err = db.Count(&count).Error
	if err != nil {
		return nil, err
	}

	//分页之后查询列表
	err = db.Offset((pageNum - 1) * pageSize).Limit(pageSize).Find(&list).Error
	//转换为 util.Page
	return util.ToPageInfo(count, pageSize, pageNum, list), err
}

// SelectById 通过Id查询出差
func (r *BusinessTripRepository) SelectById(id int, userId int) (model.BusinessTrip, error) {
	var BusinessTrip model.BusinessTrip
	err := global.DB.Where("create_by = ?", userId).Take(&BusinessTrip, id).Error
	return BusinessTrip, err
}

// BusinessTripIsUserOwned 是否仕用户所拥有的
func (r *BusinessTripRepository) BusinessTripIsUserOwned(userId int, businessId int) bool {
	var exists bool
	global.DB.Raw("SELECT EXISTS( SELECT 1 FROM business_trip WHERE id = ? AND create_by = ? )", businessId, userId).Scan(&exists)
	return exists
}

// SelectExpense 查询出差费用
func (r *BusinessTripRepository) SelectExpense(id int, expenseType int, size int, num int, userId int) (any, error) {

	if !r.BusinessTripIsUserOwned(userId, id) {
		return nil, errors.New("不是您的出差记录不可查阅！")
	}

	db := global.DB.Model(&model.BusinessTripExpense{}).Where("business_trip_id = ?", id)

	if expenseType != -1 {
		db.Where("expense_type = ?", expenseType)
	}

	var list []model.BusinessTripExpense
	var count int64

	err := db.Count(&count).Error
	if err != nil {
		return nil, err
	}

	err = db.Limit(size).Offset(size * (num - 1)).Find(&list).Error
	info := util.ToPageInfo(count, size, num, list)
	return info, err
}

// DeleteExpense 删除出差费用
func (r *BusinessTripRepository) DeleteExpense(id int, businessTripId int, userId int) error {
	if !r.BusinessTripIsUserOwned(userId, businessTripId) {
		return errors.New("不是您的出差记录不可删除！")
	}

	return global.DB.Where("business_trip_id = ?", businessTripId).Delete(&model.BusinessTripExpense{}, id).Error
}

// CreateExpense 添加出差费用
func (r *BusinessTripRepository) CreateExpense(expense model.BusinessTripExpense) error {
	return global.DB.Create(&expense).Error
}
